package com.bzkj.basicinfo.service.wms.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.bzkj.basicinfo.domain.basic.vo.WmsBillReportVo;
import com.bzkj.basicinfo.domain.ncc.NccMaterialAttribute;
import com.bzkj.basicinfo.mapper.NccMaterialAttributeMapper;
import com.bzkj.basicinfo.mapper.WmsBillReportMapper;
import com.bzkj.basicinfo.service.wms.WmsBillReportService;
import com.bzkj.common.core.domain.R;
import com.bzkj.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author swj
 * @desc
 */
@Slf4j
@Service
public class WmsBillReportServiceImpl implements WmsBillReportService {

    @Autowired
    private WmsBillReportMapper billReportMapper;

    @Autowired
    private NccMaterialAttributeMapper nccMaterialAttributeMapper;

    @Override
    public R printBillMode(String vbillcode) {
        Map<String, Object> map = new HashMap();
        List<WmsBillReportVo> otherInList = billReportMapper.queryOtherInBoundPrint(vbillcode);
        List<WmsBillReportVo> otherOutList = billReportMapper.queryOtherOutPrint(vbillcode);
        List<WmsBillReportVo> purchaseList = billReportMapper.queryPurchaseInPrint(vbillcode);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        if (CollectionUtil.isEmpty(otherInList)) {
            log.info("=======================其他入库单据打印数据为空===========================");
        } else {
            otherInList.forEach(i -> {
                String vbdef48 = i.getVbdef48();
                String dproducedate = i.getDproducedate();
                String dvalidate = i.getDvalidate();
                String dbizdate = i.getDbizdate();
                if(StringUtils.isNotBlank(dbizdate)){
                    i.setDbizdate(format.format(DateUtil.parse(dbizdate)));
                }
                if (StringUtils.isNotBlank(vbdef48)) {
                    i.setVbdef48(format.format(DateUtil.parse(vbdef48)));
                }
                if (StringUtils.isNotBlank(dproducedate)) {
                    i.setDproducedate(format.format(DateUtil.parse(dproducedate)));
                }
                if (StringUtils.isNotBlank(dvalidate)) {
                    i.setDvalidate(format.format(DateUtil.parse(dvalidate)));
                }
            });
        }
        if (CollectionUtil.isEmpty(otherOutList)) {
            otherOutList.forEach(i -> {
                //def9为pd_defdoclist数据表的code
                String def9 = i.getDef9();
                if (StringUtils.isNotBlank(def9)) {
                    //根据def获取pk数据
                    NccMaterialAttribute materialAttribute = nccMaterialAttributeMapper.queryByDef9(def9);
                    if (Objects.nonNull(materialAttribute)) {
                        String defdoclist = materialAttribute.getpK_DEFDOCLIST();
                        if (StringUtils.isNotBlank(defdoclist)) {
                            //根据pk获取name数据
                            NccMaterialAttribute attribute = nccMaterialAttributeMapper.queryByPkDefdoc(defdoclist);
                            if (Objects.nonNull(attribute) && StringUtils.isNotBlank(attribute.getName())) {
                                i.setDef9(attribute.getName());
                            }
                        }
                    }
                }

            });
            log.info("=======================其他出库单据打印数据为空===========================");
        } else {
            otherOutList.forEach(i -> {
                String vbdef48 = i.getVbdef48();
                String dproducedate = i.getDproducedate();
                String dvalidate = i.getDvalidate();
                String dbizdate = i.getDbizdate();
                if(StringUtils.isNotBlank(dbizdate)){
                    i.setDbizdate(format.format(DateUtil.parse(dbizdate)));
                }
                if (StringUtils.isNotBlank(vbdef48)) {
                    i.setVbdef48(format.format(DateUtil.parse(vbdef48)));
                }
                if (StringUtils.isNotBlank(dproducedate)) {
                    i.setDproducedate(format.format(DateUtil.parse(dproducedate)));
                }
                if (StringUtils.isNotBlank(dvalidate)) {
                    i.setDvalidate(format.format(DateUtil.parse(dvalidate)));
                }
            });
        }
        if (CollectionUtil.isEmpty(purchaseList)) {
            log.info("=======================采购入库单据打印数据为空===========================");
        } else {
            purchaseList.forEach(i -> {
                String vbdef48 = i.getVbdef48();
                String dproducedate = i.getDproducedate();
                String dvalidate = i.getDvalidate();
                String dbizdate = i.getDbizdate();
                if(StringUtils.isNotBlank(dbizdate)){
                    i.setDbizdate(format.format(DateUtil.parse(dbizdate)));
                }
                if (StringUtils.isNotBlank(vbdef48)) {
                    i.setVbdef48(format.format(DateUtil.parse(vbdef48)));
                }
                if (StringUtils.isNotBlank(dproducedate)) {
                    i.setDproducedate(format.format(DateUtil.parse(dproducedate)));
                }
                if (StringUtils.isNotBlank(dvalidate)) {
                    i.setDvalidate(format.format(DateUtil.parse(dvalidate)));
                }
            });
        }
        map.put("otherOut", otherOutList);//其他出库单-多行
        map.put("otherProductOut", otherOutList);//其他出库单（产品交接单）
        map.put("otherProductMaterialsOut", otherOutList);//其他出库单-生产原辅料
        map.put("purchaseLow", purchaseList);//低耗类采购入库单
        map.put("purchaseNotLow", purchaseList);//非低耗类采购入库单
        map.put("otherIn", otherInList);//其他入库单
        return R.ok(map);
    }
}
